﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormFormatDuplicateIdWorker
	{
        //CID 0827 //2011.03.07 //Begin
        public static void formatDuplicateId(Form form, IEnumerable idXmlNodeList, Hashtable idTable, string xctnameAttr)
        {
            foreach (XmlAttribute idAttr in idXmlNodeList)
            {
                string id = idAttr.Value;
                if (idTable.ContainsKey(id))
                {
                    ArrayList dupIdAttrList = (ArrayList)idTable[id];
                    string newId = id + JetConstant.INNER_SEPARATOR + dupIdAttrList.Count;
                    idAttr.Value = newId;
                    dupIdAttrList.Add(idAttr);
                }
                else
                {
                    ArrayList dupIdAttrList = new ArrayList();
                    dupIdAttrList.Add(idAttr);
                    idTable.Add(id, dupIdAttrList);
                }
            }

            foreach (ArrayList dupIdAttrList in idTable.Values)
            {
                if (dupIdAttrList.Count > 1 && isGroupNeedRefreshAfterUpdate(dupIdAttrList, xctnameAttr))
                {
                    foreach (XmlAttribute idAttr in dupIdAttrList)
                    {
                        formatDuplicateIdOwnerElement(idAttr, xctnameAttr);
                    }
                }
            }
        }
        //CID 0827 //2011.03.07 //End
		
		private static void  formatDuplicateIdOwnerElement(XmlAttribute idAttr, string xctnameAttr)
		{
			XmlElement parentElement = idAttr.OwnerElement;
			if (parentElement != null && isElementNeedRefreshAfterUpdate(parentElement, xctnameAttr))
			{
				parentElement.SetAttribute(JetConstant.HTML_DUPLICATE_ID, JetConstant.YES);
			}
		}
		
		private static bool isElementNeedRefreshAfterUpdate(XmlElement element, string xctnameAttr)
		{
			string tagName = element.Name.ToLower();
			if (tagName.Equals(JetConstant.HTML_div) || tagName.Equals(JetConstant.HTML_span))
			{
				return false;
			}
			else
			{

				return true;
			}
		}

		private static bool isGroupNeedRefreshAfterUpdate( ArrayList dupIdAttrList, string xctnameAttr )
		{
			ArrayList elementList = new ArrayList();
			foreach( XmlAttribute dupIdAttr in dupIdAttrList )
			{
				elementList.Add( dupIdAttr.OwnerElement );
			}

			int sectionCount = countSectionCount( elementList, xctnameAttr );
			if( elementList.Count - sectionCount == 1 )
			{
				return false;
			}

			return true;
		}

		private static int countSectionCount( ArrayList elementList, string xctnameAttr )
		{
			int count = 0;
			foreach( XmlElement element in elementList )
			{
				if( element.Name.ToLower().Equals( JetConstant.HTML_div ) )
				{
					string xctname = element.GetAttribute( xctnameAttr );
					if( xctname != null && ( xctname.Equals( JetConstant.XCT_RepeatingSection ) 
						|| xctname.Equals( JetConstant.XCT_Section ) 
						|| xctname.Equals( JetConstant.XCT_choicegroup ) ) )
					{
						count ++;
					}
				}
			}
			return count;
		}

	}
}